package net.lee.shiro.demo.config;

import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.stereotype.Component;

import javax.cache.CacheManager;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.TouchedExpiryPolicy;
import java.util.concurrent.TimeUnit;


/**
 * use the JSR-107 cache in the project this is the JSR-107 cache config in spring boot
 * if use the xml config like this
 * <config
 *     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 *     xmlns='http://www.ehcache.org/v3'
 *     xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>
 *
 *   <service>
 *     <jsr107:defaults>
 *       <jsr107:cache name="people" template="heap-cache"/>
 *     </jsr107:defaults>
 *   </service>
 *
 *   <cache-template name="heap-cache">
 *     <listeners>
 *       <listener>
 *         <class>org.terracotta.ehcache.EventLogger</class>
 *         <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
 *         <event-ordering-mode>UNORDERED</event-ordering-mode>
 *         <events-to-fire-on>CREATED</events-to-fire-on>
 *         <events-to-fire-on>UPDATED</events-to-fire-on>
 *         <events-to-fire-on>EXPIRED</events-to-fire-on>
 *         <events-to-fire-on>REMOVED</events-to-fire-on>
 *         <events-to-fire-on>EVICTED</events-to-fire-on>
 *       </listener>
 *     </listeners>
 *     <resources>
 *       <heap unit="entries">2000</heap>
 *       <offheap unit="MB">100</offheap>
 *     </resources>
 *   </cache-template>
 * </config>
 * Create by lishijun on 2018/4/11
 * Company: lianzhong
 * Domain:  ourgame.com
 * Department:Web Developer
 */

@Component
public  class CacheConfig implements JCacheManagerCustomizer {


    @Override
    public void customize(CacheManager cacheManager) {

        cacheManager.createCache("info_cache",
                new MutableConfiguration<>()
                        .setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS,10)))
                        .setStoreByValue(false).setStatisticsEnabled(true));
        //cacheManager.createCache("user_info",new MutableConfiguration<>())
    }
}
